/**
 * 测距 测面js
 * 参数：GeometryService地址
 *       默认面积单位 默认距离单位
 *
 */
//"esir/lang",  esriLang,
define(["dojo/_base/declare",
		"dojo/_base/lang",
		"esri/layers/FeatureLayer",
		"esri/InfoTemplate",
		"widget/Draw",
		"esri/symbols/SimpleFillSymbol",
		"esri/symbols/SimpleMarkerSymbol",
		"esri/symbols/SimpleLineSymbol",
		"esri/SpatialReference",
		"dojo/json",
		"esri/geometry/Geometry",
		"esri/tasks/GeometryService",
		"esri/tasks/AreasAndLengthsParameters",
		"esri/graphic",
		"esri/tasks/LengthsParameters",
		"dijit/popup",
		"dojo/dom-style",
		"dijit/TooltipDialog",
		"esri/geometry/Point",
		"widget/GraphicsLayerManager",
		"esri/geometry/geodesicUtils",
		"esri/units",
		"dijit/registry"
	],
	function(declare, lang, FeatureLayer, InfoTemplate, Draw, SimpleFillSymbol, SimpleMarkerSymbol, SimpleLineSymbol, SpatialReference, json, Geometry, GeometryService, AreasAndLengthsParameters, Graphic, LengthsParameters,
		dijitPopup, domStyle, TooltipDialog, Point, GraphicsLayerManager,geodesicUtils,Units,registry) {
		return declare(null, {
			geometryService: null,
			areasAndLengthParams: null,
			map: null,
			draw: null,
			geometryUrl:dojoConfig.layers["geometryServer"].url,
			lengthParams: null,
			geometry: null,
			constructor: function(params) {
				declare.safeMixin(this, params);
				var map = this.map;
				this.geometryService = new GeometryService(this.geometryUrl);
				Draw.getInstance(map);
				//var draw=Draw.getInstance();
				this.geometryService.on("areas-and-lengths-complete", lang.hitch(this, this.outputAreaAndLength));
				map.infoWindow.resize(245, 125);

			},
			activate:function(type){
				debugger;
				var map = this.map;
				map.setInfoWindowOnClick(false);
				map.infoWindow.hide();
				Draw.setDrawEnd(lang.hitch(this, this.getAreaAndLength)); //lang.hitchb在MAP范围内执行getAreaAndLength方法
				Draw.setActivate(type);
			},
			disactivate:function(){
				var map = this.map;
				map.setInfoWindowOnClick(true);
				Draw.deactivate();
			},
			getAreaAndLength: function(evtObj) {
				var map = this.map;
				this.geometry = evtObj.geometry;
				this.disactivate();
					if(this.geometry.type == "polyline" || this.geometry.type == "line") {
						this.getLengths();
					} else if(this.geometry.rings.length!=0 && this.geometry.rings[0].length<=3){
						this.errorCallBack();
					}else{
						this.areasAndLengths();
					}

				//map.graphics.clear();
				//每次测量完成恢复到平移状态
					if( typeof  this.successcallBack=="function"){
						this.successcallBack()
					}
			},
			getLengths: function() {
				this.lengthParams = new LengthsParameters();
				this.lengthParams.polylines = [this.geometry];
				this.lengthParams.lengthUnit = GeometryService.UNIT_METER;
				this.lengthParams.geodesic = true;
				var self = this;
				var length = this.geometry.paths[0].length;
				var lengths = geodesicUtils.geodesicLengths([this.geometry], Units.METERS);
				var info="长度:" +Number(lengths).toFixed(1)+ "米";
				self.showResult(info);
		/* 		this.geometryService.lengths(this.lengthParams, function(evt) {
					var hh = evt;
					var length;
					if(evt.lengths[0] > 100) {
						length = evt.lengths[0] / 1000;
						length = length.toFixed(3)+ "千米";
					} else {
						length = evt.lengths[0].toFixed(3) + "米";
					}
					var cont = "长度" + length;
					self.showResult(cont);

				}); */

			},
			areasAndLengths: function() {
				this.areasAndLengthParams = new AreasAndLengthsParameters();
				this.areasAndLengthParams.lengthUnit = GeometryService.UNIT_METER; //英尺
				this.areasAndLengthParams.areaUnit = GeometryService.UNIT_SQUARE_METERS; //英亩
				this.areasAndLengthParams.calculationType = "preserveShape"; //定义了几何计算的类型,使用这种计算面积或长度仅使用多边形的顶点定义线连接的顶点作为测地线段独立的实际形状的多边形
				var self = this;
				var areas = geodesicUtils.geodesicAreas([this.geometry], Units.SQUARE_METERS);
				var cont = "面积："+ Number(areas).toFixed(1)+"平方米";
				this.showResult(cont);
				
			/* 	this.geometryService.simplify([this.geometry], function(simplifiedGeometries) { //返回了一系列的简化几何对象，确保拓扑关系正确
					self.areasAndLengthParams.polygons = simplifiedGeometries;
					self.geometryService.areasAndLengths(self.areasAndLengthParams);
				}); */
			},

			outputAreaAndLength: function(evtobj) {
				var result = evtobj.result;
				var ereas, length;
				if(result.areas[0]> 10000) {
					ereas = result.areas[0] / 1000000;
					ereas = ereas.toFixed(3)+ "平方千米";
				} else {
					ereas = result.areas[0].toFixed(3) + "平方米";
				}
				if(result.lengths[0].toFixed(3) > 100) {
					length = result.lengths[0] / 1000;
					length = length.toFixed(3)+ "千米";
				} else {
					length = result.lengths[0].toFixed(3) + "米";
				}
				var cont = "面积：" + ereas + "<br/>" + "周长：" + length;
				this.showResult(cont);
			},
			showResult: function(content) {
				var point;
				var symbol;
				switch(this.geometry.type) {
					case "point":
					case "multipoint":
						symbol = new SimpleMarkerSymbol();
						break;
					case "polyline":
						var len = this.geometry.paths[0].length;
						point = new Point(this.geometry.paths[0][len - 1], this.map.spatialReference);
						symbol = new SimpleLineSymbol();
						break;
					default:
						point = new Point(this.geometry.rings[0][0], this.map.spatialReference);
						symbol = new SimpleFillSymbol();
						break;
				}

				var attr = {
					"Xcoord": content,
				};
				var self=this;
				var graphic=new Graphic(this.geometry, symbol, attr);
				var measureLayer=GraphicsLayerManager.getGraphicLayer("measureLayer",this.map);
				if(!measureLayer){
					measureLayer=GraphicsLayerManager.addGraphicLayer({id:"measureLayer",map:this.map});
				}
				if(!measureLayer.infoTemplate){
					var infoTemplate = new InfoTemplate("测量结果", "${Xcoord}");
					measureLayer.setInfoTemplate(infoTemplate);
				}
				this.map.infoWindow.setContent(content);
				this.map.infoWindow.setTitle("测量结果");
				this.map.infoWindow.show(point);
				measureLayer.add(graphic);
			},
			destroy:function(){
				this.map.infoWindow.hide();
				this.map.graphics.clear();
				this.disactivate();
			}
		});

	});
